package com.tuo51.evaluate.repositories.impl;

import com.tuo51.evaluate.repositories.evaluatev2.EvaluateV2ToolNativeRepository;
import com.tuo51.evaluate.vo.evaluate.EvaluateV2ToolVO;
import com.matt.repository.base.BaseNativeSqlRepository;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@CacheConfig(cacheNames = "EvaluateV2ToolNativeRepositoryImpl")
public class EvaluateV2ToolNativeRepositoryImpl extends BaseNativeSqlRepository implements EvaluateV2ToolNativeRepository {
    @Override
    @Cacheable(keyGenerator = "keyGenerator")
    public List<EvaluateV2ToolVO> findAllTools(Integer month) {

        String monthStr = "";
        if(month != null && month.intValue() > 0){
            monthStr = "and month >= " + month;
        }

        String querySql = "SELECT t.id,t.title,t.count, t.unit,t.comment,t.imgUrl,t.imgCount,t.url,q.month,q.indexOnPaper" +
                " FROM tb_evaluate_tool t left join tb_evaluate_question q on t.questionId = q.id " +
                " where t.count > 0 and t.imgUrl is not null AND length(t.title) > 0 " + monthStr +
                " group by t.title order by q.indexOnPaper ";

        List<EvaluateV2ToolVO> objectArrayList = sqlObjectList(querySql, EvaluateV2ToolVO.class);

        return objectArrayList;
    }
}
